//
//  ViewController.swift
//  TPFormater
//
//  Created by 谭鹏 on 2018/2/1.
//  Copyright © 2018年 谭鹏. All rights reserved.
//

//序号    编码    类型    是否必填    描述    备注
//1    empl    int    n    员工ID    不传这是用户信息
//2    status    int    y    查询类型    1 发货记录， 2已收货，3待收货记录
//2    deliverystatus    int    n    物流状态    1发货 2到达 3签收 4退回 5损坏 6丢失 7退回签收
//3    dstarttime    string    n    发货开始时间    ex starttime:"2018-01-10"
//4    dendtime    string    n    发货截至时间    ex endtime:"2018-01-10"
//5    rstarttime    string    n    收货开始时间    ex starttime:"2018-01-10"
//6    rendtime    string    n    收货截至时间    ex endtime:"2018-01-10"
//7    sendperson    string    n    发货人
//8    receivperson    string    n    收货人
import Cocoa

class ViewController: NSViewController {
    @IBOutlet var textView_input: NSTextView!
    
    @IBOutlet var textView_output: NSTextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        
        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func onBtn_Convert(_ sender: Any) {
        
        let text = self.textView_input.string
        var  elements = text.split(separator: "\n",  omittingEmptySubsequences: false)
        if let first = elements.first,first.isEmpty{
            elements.remove(at: 0)
        }
        var result = ""
        for i in stride(from: 0, to: elements.count, by: 6) {
            let type = (elements[safe:i+2] ?? "").capitalizingFirstLetter()
            let variable = elements[safe:i+1] ?? ""
            let desc = "\(elements[safe:i+3] ?? "") \(elements[safe:i+4] ?? "") \(elements[safe:i+5] ?? "" )"
            
            
            if type.count > 0,variable.count > 0 {
                result += ("var \(variable):\(type)? // \(desc) \n" )
            }
            
            
            
        }
        print(elements.count )
        print(elements)
        print(result)
        self.textView_output.string = result
        
        
        
    }
    
}
public extension Collection {
    
    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}
extension Substring{
    func capitalizingFirstLetter() -> String {
        return prefix(1).uppercased() + dropFirst()
    }
}

